package com.shop.service.impl;

import com.shop.dao.OrderMapper;
import com.shop.dao.ProductAttributeMapper;
import com.shop.dao.ProductMapper;
import com.shop.dao.StoreMapper;
import com.shop.entity.ProductAttribute;
import com.shop.entity.Store;
import com.shop.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    @Override
    public Double getUserPay(Long userId, int month) {
        Double money = 0.0;
        List list = orderMapper.selectByBuyerIdAndMonth(userId, month);
        for (Object o : list) {
            Map<String, Object> map = (HashMap<String, Object>) o;
            money += Double.parseDouble(String.valueOf(map.get("unit_price")))
                    * Integer.parseInt(String.valueOf(map.get("good_quantity")));
        }
        return money;
    }

    @Override
    public int getUserShopTime(Long userId, int month) {
        List list = orderMapper.selectByBuyerIdAndMonth(userId, month);
        return list.size();
    }

    @Override
    public List getSlider() {
        List list = productMapper.selectLimitFive();
        List result = new ArrayList<>();
        for (Object o : list) {
            Map resultMap = new HashMap();
            Map map = (HashMap<String, String>) o;
            ProductAttribute productAttribute = productAttributeMapper.findOne(Long.valueOf(String.valueOf(map.get("product_id"))));
            resultMap.put("productId", map.get("product_id"));
            resultMap.put("storeId", map.get("store_id"));
            resultMap.put("img", productAttribute.getImgPath());
            result.add(resultMap);
        }
        return result;
    }

    @Override
    public Double getOrderEarn(Long userId, int month) {
        Double money = 0.0;
        List<Store> allStoreByUserId = storeMapper.findAllStoreByUserId(userId);
        for(Store s:allStoreByUserId) {
            List list = orderMapper.selectByStoreIdAndMonth(s.getStoreId(), month);
            for (Object o : list) {
                Map<String, Object> map = (HashMap<String, Object>) o;
                money += Double.parseDouble(String.valueOf(map.get("unit_price")))
                        * Integer.parseInt(String.valueOf(map.get("good_quantity")));
            }
        }
        return money;
    }

    @Override
    public int getOrderSum(Long userId, int month) {
        int sum=0;
        List<Store> allStoreByUserId = storeMapper.findAllStoreByUserId(userId);
        for(Store s:allStoreByUserId) {
            List list = orderMapper.selectByStoreIdAndMonth(s.getStoreId(), month);
            sum+=list.size();
        }
        return sum;
    }
}
